/*
 * Pada - C# Implementation
 * Based on original work of Michael Liu (SDET Consulting Inc : http://sdet.ca)
 * Author: Michael Liu (http://sdet.ca)
 * Author: Rudy Lattae
 * Version: 0.5-b
 */

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

namespace Pada
{	

	// Main Form for the Pada Application
	public class PadaForm : Form
	{
		private TextBox textBox1;			// textbox for editing
		private string currentFile;			// path to current file being edited
		
		
		public PadaForm()
		{
			InitForm();
		}
		
		public PadaForm(string file)
		{
			InitForm();
			ReadFile(file);
		}
		
		
		/// <summary>
		/// Initialize the main form and its componenets
		/// </summary>
		private void InitForm()
		{
			// Initialize the properties for the main form
			this.Text = "Pada";
            this.Opacity = 0.9; 
            this.Width = 500;  
            this.Height = 400;
			
			// initialize the text box
			textBox1 = new TextBox();
			textBox1.Name = "textBox1";
			textBox1.Location = new Point(10, 8);
			textBox1.Dock = DockStyle.Fill;
			textBox1.ScrollBars = ScrollBars.Both;
			textBox1.Font = new Font("Lucida Console", 14);
			textBox1.BackColor = Color.Black;
            textBox1.ForeColor = Color.GreenYellow;
			textBox1.AcceptsReturn = true;
			textBox1.AcceptsTab = true;
			textBox1.Multiline = true;
			textBox1.WordWrap = false;
			textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
			
			Controls.Add(textBox1);
		}
		
		/// <summary>
		/// Handle Keydown events on the textbox
		/// </summary>
		private void textBox1_KeyDown(object sender, KeyEventArgs e)
		{
			// file - new
			if (e.Control && e.KeyCode == Keys.N) NewFile();
			
			// file - open
			if (e.Control && e.KeyCode == Keys.O) OpenFile();
			
			// file - save
			if (e.Control && e.KeyCode == Keys.S) SaveFile();
			
			// Quit
			if (e.Control && e.KeyCode == Keys.Q) this.Close();
			
			
			// opacity - increase
			if (e.Control && e.KeyCode == Keys.Down) this.Opacity += .05;
			
			// opacity - decrease
			if (e.Control && e.KeyCode == Keys.Up) this.Opacity -= .05;
				
			// font size - increase
			if (e.Alt && e.KeyCode == Keys.Up)
                textBox1.Font = new Font(this.textBox1.Font.Name, this.textBox1.Font.Size + 1);
		     
			// font size - decrease
			if (e.Alt && e.KeyCode == Keys.Down)
                textBox1.Font = new Font(this.textBox1.Font.Name, this.textBox1.Font.Size - 1);
			
			// toggle word wrap
			if (e.Control & e.KeyCode == Keys.Enter)
                this.textBox1.WordWrap = !this.textBox1.WordWrap;
			
			// select all text
			if (e.Control & e.KeyCode == Keys.A) this.textBox1.SelectAll();
		}
		
		/// <summary>
		/// Updates the caption of the editor to reflect the name and status of the current text/file opened
		/// </summary>
		private void UpdateCaption()
		{
			if (currentFile != null) {
				this.Text = currentFile + " - Pada";
			} else {
				this.Text = "Pada";
			}
		}
		
		
		/// <summary>
		/// Creates a new file
		/// </summary>
		public void NewFile()
		{
			// TODO: "New File" probably should just open a new for instead of clearing out everything the user has typed in.
			textBox1.Text = "";
			currentFile = null;
			UpdateCaption();
		}
		
		/// <summary>
		/// Opens a file for editing
		/// </summary>
		public void OpenFile()
		{
			using (OpenFileDialog dialog = new OpenFileDialog()) {
				dialog.Title = "Open the pandora box";
				dialog.Filter = "Text|*.txt|VB|*.vb|C#|*.cs|HTML|*.html|XML|*.xml|C|*.c|C++|*.cpp|Header|*.h|Perl|*.pl|VBScript|*.vbs|JavaScript|*.js|Log File|*.log|All|*.*";
				
				if (dialog.ShowDialog() == DialogResult.Cancel) return;
				
				ReadFile(dialog.FileName);
            }
		}
		
		/// <summary>
		/// Reads the given file  and display its contents
		/// </summary>
		public void ReadFile(string file)
		{
			textBox1.Text = File.ReadAllText(file);
			currentFile = file;			
			UpdateCaption();
		}

		/// <summary>
		/// Saves the current text to the opened file. If there is no currently opened file, user is prompted to "Save As"
		/// </summary>
		public void SaveFile()
		{
			if (currentFile != null) WriteFile(currentFile);
			else SaveFileAs();
		}
		
		/// <summary>
		/// Prompts the user for a file and saves the current text to the given file
		/// </summary>
		public void SaveFileAs()
		{
			using (SaveFileDialog dialog = new SaveFileDialog()) {
				dialog.Title = "Save your work, save the world!";
				dialog.Filter = "Text|*.txt|VB|*.vb|C#|*.cs|HTML|*.html|XML|*.xml|C|*.c|C++|*.cpp|Header|*.h|Perl|*.pl|VBScript|*.vbs|JavaScript|*.js|Log File|*.log|All|*.*";
				
				if (dialog.ShowDialog() == DialogResult.Cancel) return;
				
				WriteFile(dialog.FileName);				
			}
		}
		
		/// <summary>
		/// Writes the given file to disk
		/// </summary>
		public void WriteFile(string file)
		{
			File.WriteAllText(file, textBox1.Text);
			currentFile = file;
			UpdateCaption();
		}

	}
	
	// Launcher for the Pada Application
	public class PadaApp
	{
		public static void Main(string[] args)
		{
			if (args.Length > 0 && File.Exists(args[0]))	
				Application.Run(new PadaForm(args[0]));
			else
				Application.Run(new PadaForm());
		}
	}	
}
